package com.supreme.studyserver.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

@Component
public class QumBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    private static ConfigurableListableBeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("QumBeanFactoryPostProcessor");
        QumBeanFactoryPostProcessor.beanFactory = configurableListableBeanFactory;
        if (configurableListableBeanFactory.containsBean("authUser")) {
            System.out.println("QumBeanFactoryPostProcessor => authUser");
            final BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition("authUser");
            beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
        }
    }

    public static <TB> TB getBean(Class<TB> clazz) {
        return QumBeanFactoryPostProcessor.beanFactory.getBean(clazz);
    }
}
